package com.edu.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * 统一日志处理 ：打印日志的拦截器
 * 
 * @author Administrator
 *
 */
public class LoggerMethodInterceptor implements MethodInterceptor {

	private final static ObjectMapper jsonMapper = new ObjectMapper();
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {

		logger.info("Before: interceptor name: {}", invocation.getMethod().getName());

		logger.info("Arguments: {}", jsonMapper.writeValueAsString(invocation.getArguments()));

		Object result = invocation.proceed();

		logger.info("After: result: {}", jsonMapper.writeValueAsString(result));

		return result;
	}

}
